import {Component, Input, OnInit} from "@angular/core";
import {LocalDataSource} from "ng2-smart-table";

@Component({
  selector:'my-smart-table',
  template:`
    <nb-card>
      <nb-card-header>
        {{title}}
      </nb-card-header>
      <nb-card-body>
        <ng2-smart-table [settings]="settings" [source]="source" (deleteConfirm)="onDeleteConfirm($event)" (editConfirm)="onEditConfirm($event)">
        </ng2-smart-table>
      </nb-card-body>
      <nb-card-footer>
        <div class="float-right">
          <div class="form-inline">
            <div class="form-group">
              <input class="form-control input-sm" type="button" value="上一页" [disabled]="page==1" (click)="_goto(page-1)">
              <input class="form-control input-sm" style="width: 60px" type="text" [value]="page" #gotoPage>
              <span>{{' /' + maxPage.toString() + ' '}}</span>
              <input class="form-control input-sm" type="button" value="前往" (click)="_goto(gotoPage.value-0)">
              <input class="form-control input-sm" type="button" value="下一页" (click)="_goto(page+1)">
            </div>
          </div>
        </div>
      </nb-card-footer>
    </nb-card>
  `
})
export class MySmartTable implements OnInit{
  @Input() public addable:boolean = true;
  @Input() public editable:boolean = true;
  @Input() public deletable:boolean = true;
  @Input() public onDeleteConfirm:(any)=>void;
  @Input() public onEditConfirm:(any)=>void;
  @Input() public goto:(number)=>Promise<{dataCount:number,data:any[]}>;
  @Input() public title:string = '';
  @Input() public limit:number = 25;
  settings = {
    hideSubHeader: true,
    actions: {
      columnTitle: '操作',
      add: false,
      edit:this.editable,
      delete:this.deletable,
      position: 'right'
    },
    edit: {
      editButtonContent: '<i class="nb-edit"></i>',
      saveButtonContent: '<i class="nb-checkmark"></i>',
      cancelButtonContent: '<i class="nb-close"></i>',
      confirmSave: true,
    },
    delete: {
      deleteButtonContent: '<i class="nb-trash"></i>',
      confirmDelete: true,
    },
  };
  source = new LocalDataSource();
  maxPage: number = 1;
  page: number = 1;
  constructor(){
    this._goto(this.page);
  }

  ngOnInit(): void {

  }

  _goto(page:number){
    this.goto(page).then(data=>{
      this.source.load(data.data);
      this.maxPage = Math.ceil(data.dataCount / this.limit);
    });
    this.page = page;
  }
}
